import config from '../config'
export default async function request(url,options){
  const baseUrl = config.baseUrl
  // token相关待定
  let defaultOptions = {
    headers: new Headers({"Content-Type": "application/json;charset=utf-8"}),
    method: 'post',
    mode: "cors"
  }
  options = Object.assign(defaultOptions,options)
  console.log(options)
  if(options.method == 'post'){
    // 默认post请求
    try {
      // 注意这里的await语句，其所在的函数必须有async关键字声明
      options.body = JSON.stringify(options.body);
      console.log("请求url",baseUrl+url)
      var response = await fetch(baseUrl+url, options)
      if (response.ok) {
        var result = await response.json()
        console.log("服务器返回值",result)
        return result;
      }
    } catch(error) {
      console.error(error);
    }
  }else{
    try {
      // 注意这里的await语句，其所在的函数必须有async关键字声明
      if(options.body){
        url = url +'?'+options.body
      }
      console.log("请求url",baseUrl+url)
      var response = await fetch(baseUrl+url, options)
      if (response.ok) {
        var result = await response.json()
        console.log("服务器返回值",result)
        return result;
      }
    } catch(error) {
      console.error(error);
    }
  }
}
